<template>
  <div class="news-classify">
    <div style="display:flex;align-items: center;margin:0 0 10px;">
      <el-button size="small" type="primary" @click="add">新建分类</el-button>
    </div>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
      <el-table-column label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="分类名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="分类排序">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="分类级别">
        <template>
          1级
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
          <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="openChild(scope.row)" type="text">下一级</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button @click="changeState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
          <el-button @click="changeState(scope.row)" type="text" v-else>启用</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button @click="edit(scope.row)" type="text">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            @onConfirm="del(scope.row)"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="是否删除本条数据？">
            <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:10px 0 0 0;text-align:right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 30, 50, 100]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageObj.total">
      </el-pagination>
    </div>

    <el-dialog :title="dailogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item style="margin-bottom:20px" label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="分类排序" :label-width="formLabelWidth">
          <el-input v-model="form.sort" autocomplete="off" placeholder="请输入分类名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="dailogChildTitle" :visible.sync="dialogFormChildVisible">
      <el-form :model="formChild">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="formChild.name" autocomplete="off" placeholder="请输入分类名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelChild">取 消</el-button>
        <el-button type="primary" @click="saveChild">确 定</el-button>
      </div>
    </el-dialog>

    <el-drawer
      :title="`上级分类名：${beforeName}`"
      direction="rtl"
      size="1000px"
      :visible.sync="childModal">
      <div style="padding:0 30px;">
        <div style="margin-bottom:20px;">
          <el-button @click="goBack" size="small" type="primary">返回上一级</el-button>
          <el-button @click="addChild" size="small" type="primary">新建分类</el-button>
        </div>
        <el-table ref="multipleTable" height="80vh" v-loading="childrenLoading" :data="childList" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
          <el-table-column label="ID" width="100">
            <template slot-scope="scope">
              {{ scope.row.id }}
            </template>
          </el-table-column>
          <el-table-column label="分类名称">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column label="分类级别">
            <template>
              {{ typeNum+1 }}级
            </template>
          </el-table-column>
          <el-table-column label="创建时间" width="180">
            <template slot-scope="scope">
              {{ scope.row.createTime }}
            </template>
          </el-table-column>
          <el-table-column label="启用状态" width="100">
            <template slot-scope="scope">
              <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
              <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="240">
            <template slot-scope="scope">
              <el-button @click="openChild(scope.row)" type="text">下一级</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button @click="changeChildState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
              <el-button @click="changeChildState(scope.row)" type="text" v-else>启用</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button @click="editChild(scope.row)" type="text">编辑</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-popconfirm
                @onConfirm="delChild(scope.row)"
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                icon-color="red"
                title="是否删除本条数据？">
                <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

      </div>
    </el-drawer>

  </div>
</template>

<script>
import {
  gettypeListApi,
  saveTypeApi,
  changeTypeStateApi,
  getTypeDetailApi,
  deleteTypeApi,
  getChildrenApi
} from '@/api/column'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dailogTitle: '新建分类',
      form: {
        name: '',
        sort: '',
      },
      formLabelWidth: '80px',
      currentPage: 1,
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      saveId: '',
      parentId: '',
      parentName: '',
      fullName: '',
      childModal: false,
      childList: [],
      childrenLoading: false,
      dailogChildTitle: '新建分类',
      dialogFormChildVisible: false,
      formChild: {
        name: '',
      },
      beforeName: '',
      saveChildId: '',
      childName: '',
      typeNum: 0,
      backId: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    goBack() {
      if(this.typeNum == 1) {
        this.childModal = false
        this.typeNum = 0
      }else{
        this.typeNum--
        this.parentId = this.backId
        this.getChildrenList(this.backId)
      }
    },
    openChild(row) {
      this.childList = []
      this.typeNum++
      this.childModal = true
      this.parentId = row.id
      this.backId = row.parentId
      this.beforeName = row.fullName
      this.childName = row.name
      this.getChildrenList(row.id)
    },
    getChildrenList(id) {
      this.childrenLoading = true
      getChildrenApi(id).then(response => {
        this.childrenLoading = false
        this.childList = response.data
      })
    },
    onSubmit() {
      this.fetchData()
    },
    del(row) {
      deleteTypeApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    delChild(row) {
      deleteTypeApi({
        ids: row.id
      }).then((res) => {
        this.getChildrenList(this.parentId)
      })
    },
    addChild() {
      this.dialogFormChildVisible = true
      this.formChild.name = ''
      this.saveChildId = ''
      this.dailogChildTitle = '新建分类'
    },
    add() {
      this.dialogFormVisible = true
      this.form.name = ''
      this.form.sort = ''
      this.parentId = ''
      this.saveId = ''
      this.dailogTitle = '新建分类'
    },
    edit(row) {
      this.dailogTitle = '编辑分类'
      this.saveId = row.id
      getTypeDetailApi(row.id).then((res) => {
        this.dialogFormVisible = true
        this.form.name = res.data.name
        this.form.sort = res.data.sort
      })
    },
    editChild(row) {
      this.dailogChildTitle = '编辑分类'
      this.saveChildId = row.id
      getTypeDetailApi(row.id).then((res) => {
        this.dialogFormChildVisible = true
        this.formChild.name = res.data.name
      })
    },
    fetchData() {
      this.listLoading = true
      gettypeListApi({
        title: this.form.title,
        state: this.form.state,
        pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum,
      }).then(response => {
        this.list = response.data.result
        this.pageObj.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    },
    save() {
      saveTypeApi({
        id: this.saveId,
        name: this.form.name,
        sort: this.form.sort,
        fullName: this.form.name
      }).then((res) => {
        this.$message.success('操作成功！');
        this.cancel()
        this.fetchData()
      })
    },
    saveChild() {
      let fullName = ''
      fullName = `${this.beforeName}/${this.formChild.name}`
      saveTypeApi({
        id: this.saveChildId,
        name: this.formChild.name,
        parentId: this.parentId,
        // fullName: `${this.beforeName}/${this.formChild.name}`
        fullName: fullName
      }).then((res) => {
        this.$message.success('操作成功！');
        this.dialogFormChildVisible = false
        this.getChildrenList(this.parentId)
      })
    },
    cancelChild() {
      this.formChild.name = ''
      this.saveChildId = ''
      this.dialogFormChildVisible = false
    },
    cancel() {
      this.saveId = ''
      this.fullName = ''
      this.parentId = ''
      this.dialogFormVisible = false
      this.typeNum = 0
    },
    changeState(row) {
      changeTypeStateApi({
        id: row.id,
        state: row.state == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
      })
    },
    changeChildState(row) {
      changeTypeStateApi({
        id: row.id,
        state: row.state == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.getChildrenList(this.parentId)
      })
    },
    changeIsShow(row) {
      changeTypeStateApi({
        id: row.id,
        isShow: row.isShow == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
      })
    },
  }
}
</script>

<style lang="scss" scoped>
   .news-classify{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
